package com.qiwx.pigeon.activity.account;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.andview.refreshview.XRefreshView;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.Utils.SharedPreferencesUtil;
import com.qiwx.pigeon.adapter.account.EmpolyAdapter;
import com.qiwx.pigeon.base.BaseMVPActivity;
import com.qiwx.pigeon.data.params.GetMemberParam;
import com.qiwx.pigeon.data.vo.EmployBean;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.presenter.UserManagerPresenter;
import com.qiwx.pigeon.viewinterface.IUserManagerView;
import com.qiwx.pigeon.widget.ArrowView;
import com.qiwx.pigeon.widget.itemderection.LineItemDecoration;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @description
 * @Author qiwx
 * @Date 2020-06-06 11:10
 * 下属账号管理
 **/
public class UserManagerActivity extends BaseMVPActivity<IUserManagerView, UserManagerPresenter> implements IUserManagerView {
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_return)
    ArrowView titleReturn;
    @BindView(R.id.title_right)
    TextView titleRight;
    @BindView(R.id.txt_nodata)
    TextView txtNodata;
    @BindView(R.id.lin_nodata)
    LinearLayout linNodata;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.xRefreshView)
    XRefreshView xRefreshView;
    @BindView(R.id.rel_nodata_recycleview)
    RelativeLayout relNodataRecycleview;
    EmployBean employBean;
    boolean isFresh = true;
    int pageSize = 50;//每页显示20条记录
    int pageNum = 1;
    List<EmployBean> list = new ArrayList<>();
    EmpolyAdapter adapter;
    GetMemberParam param = new GetMemberParam();

    @Override
    public UserManagerPresenter createPresenter() {
        return new UserManagerPresenter(new WeakReference(this));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_user_manager;
    }

    @Override
    public void initAfterOnCreate() {
        employBean = SharedPreferencesUtil.getEmployBean(this);
        if (employBean == null) {
            MyToast.showShort("重新登录");
            finish();
        }
        param.setPageSize(pageSize);
        param.setId(employBean.getId());
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData(true);
    }

    private void initView() {
        titleReturn.changeArrowLeft();
        titleContent.setText("账号管理");
        titleRight.setText("添加账号");
        titleRight.setVisibility(View.VISIBLE);
        xRefreshView.setHideFooterWhenComplete(true);
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh(boolean isPullDown) {
                loadData(true);

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                pageNum++;
                loadData(false);
            }
        });
        adapter = new EmpolyAdapter(R.layout.adapter_employ_list_item, list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
//        int space = ScreenUtils.dip2px(getActivity(), );
        recyclerview.addItemDecoration(new LineItemDecoration(1));
        recyclerview.setAdapter(adapter);
        adapter.setICageInterface((pos, status, methodId) -> {
        });
    }

    private void loadData(boolean isFreash) {
        this.isFresh = isFreash;
        if (isFreash) {
            list.clear();
            pageNum = 1;
        }
        param.setPageNum(pageNum);
        mPresenter.getMembers(param);
    }

    @Override
    public void onMemberSuccess(PageInfo<EmployBean> datas) {
        list.addAll(datas.getList());
        adapter.notifyDataSetChanged();
        if (!datas.isHasNextPage()) {
            xRefreshView.setPullLoadEnable(false);
            if (!isFresh)
                MyToast.showLong("没有更多数据了");
        } else {
            xRefreshView.setPullLoadEnable(true);
        }
        if (isFresh) {
            xRefreshView.stopRefresh();
        } else {
            xRefreshView.stopLoadMore();

        }

    }

    @Override
    public void onSuccess(int code) {

    }

    @Override
    public void onFailure(String error) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.title_return, R.id.title_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_return:
                finish();
            case R.id.title_right:
                startActivity(new Intent(this, AddUserActivity.class));
                break;
        }
    }
}
